/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *
 */
package org.cholm.media.castdr.backend;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cholm
 */
public class Category {

    /**
     * Name of category
     */
    protected String mName = null;
    /**
     * Mapping of sub-categories
     */
    protected Map<String, SubCategory> mMap = new HashMap<String, SubCategory>();
    /**
     * Our sorted index into map
     */
    protected List<String> mIndex = new ArrayList<String>();

    /**
     *
     */
    public Category(String name) {
        mName = name;
    }

    /**
     * Get a sub-category
     *
     * @param name Name of sub-category
     * @return Sub-category
     */
    public SubCategory getSub(String name) {
        SubCategory ret = mMap.get(name);
        return ret;
    }

    /**
     * Get a sub-category
     *
     * @param idx
     * @return Sub-category
     */
    public SubCategory getSub(int idx) {
        if (idx < 0 || idx >= mIndex.size()) return null;
        String name = mIndex.get(idx);
        return getSub(name);
    }

    /**
     * Add a sub-category
     *
     * @param sub Sub-category
     */
    public void addSub(SubCategory sub) {
        mMap.put(sub.getName(), sub);
        mIndex.add(sub.getName());
    }

    /**
     * Sort our index
     */
    public void sortIndex() {
        Collator c = Collator.getInstance(Extractor.DANISH);
        c.setStrength(Collator.SECONDARY);
        Collections.sort(mIndex);
    }

    /**
     * Add a serie to a sub-cateogory
     *
     * @param subName Sub-category name
     * @param serie   Serie to add
     */
    public void addSerie(String subName, Serie serie) {
        SubCategory sub = getSub(subName);
        if (sub == null) return;
        sub.addSerie(serie);

    }

    /**
     * Get the mapping
     *
     * @return The map
     */
    public Map<String, SubCategory> getMap() {
        return mMap;
    }

    /**
     * Get the name
     *
     * @return The name
     */
    public String getName() {
        return mName;
    }
}
//
// EOF
//
